<template>
  <div class="edit-book-container">
    <h1>修改书籍信息</h1>
    <div class="search-box">
      <input
        v-model="searchTerm"
        type="text"
        placeholder="输入图书名称..."
        class="search-input"
        @keyup.enter="searchBooks"
      />
      <button @click="searchBooks" class="search-button">查询</button>
    </div>

    <div v-if="loading" class="loading">加载中...</div>

    <div v-if="!loading && books.length > 0" class="book-results">
      <div v-for="book in books" :key="book.id" class="book-card">
        <h3>{{ book.title }}</h3>
        <p>作者: {{ book.author }}</p>
        <p>出版社: {{ book.publisher }}</p>
        <p>出版日期: {{ book.publishDate }}</p>
        <p>简介: {{ book.description }}</p>
        <button @click="editBook(book)" class="edit-button">编辑</button>
      </div>
    </div>

    <div v-if="!loading && books.length === 0 && searched" class="no-results">
      <p>未找到相关图书，请尝试其他关键词。</p>
    </div>

    <!-- 编辑书籍表单 -->
    <div v-if="selectedBook" class="edit-form">
      <h2>编辑书籍信息</h2>
      <form @submit.prevent="updateBook">
        <div class="form-group">
          <label for="edit-title">书名</label>
          <input
            type="text"
            id="edit-title"
            v-model="selectedBook.title"
            required
          />
        </div>
        <div class="form-group">
          <label for="edit-author">作者</label>
          <input
            type="text"
            id="edit-author"
            v-model="selectedBook.author"
            required
          />
        </div>
        <div class="form-group">
          <label for="edit-publisher">出版社</label>
          <input
            type="text"
            id="edit-publisher"
            v-model="selectedBook.publisher"
            required
          />
        </div>
        <div class="form-group">
          <label for="edit-publishDate">出版日期</label>
          <input
            type="date"
            id="edit-publishDate"
            v-model="selectedBook.publishDate"
            required
          />
        </div>
        <div class="form-group">
          <label for="edit-description">简介</label>
          <textarea
            id="edit-description"
            v-model="selectedBook.description"
            required
          ></textarea>
        </div>
        <button type="submit">保存修改</button>
        <button @click="cancelEdit" class="cancel-button">取消</button>
      </form>
    </div>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  name: "EditBook",
  data() {
    return {
      searchTerm: '',
      books: [],
      loading: false,
      searched: false,
      selectedBook: null, // 当前正在编辑的书籍
    };
  },
  methods: {
    async searchBooks() {
      if (!this.searchTerm.trim()) {
        return;
      }

      this.loading = true;
      this.searched = true;

      try {
        const response = await axios.get(`http://10.70.119.172:9999/user/test?bookName=${this.searchTerm}`);
        this.books = response.data.map(book => ({
          id: book.bookId,
          title: book.title,
          author: book.author,
          publisher: book.publisher,
          publishDate: book.publishDate,
          description: book.description
        }));
      } catch (error) {
        console.error('图书查询失败:', error);
        this.books = [];
      } finally {
        this.loading = false;
      }
    },
    editBook(book) {
      this.selectedBook = { ...book }; // 复制书籍信息到编辑表单
    },
    async updateBook() {
      try {
        await axios.put(`http://10.70.119.172:9999/user/updateBook/${this.selectedBook.id}`, this.selectedBook);
        this.$alert('书籍信息修改成功');
        this.selectedBook = null; // 关闭编辑表单
        this.searchBooks(); // 刷新书籍列表
      } catch (error) {
        console.error('修改书籍失败:', error);
        this.$alert('修改书籍失败，请稍后重试。');
      }
    },
    cancelEdit() {
      this.selectedBook = null; // 取消编辑，关闭表单
    }
  }
};
</script>

<style scoped>
.edit-book-container {
  max-width: 800px;
  margin: 0 auto;
  padding: 20px;
}

h1, h2 {
  text-align: center;
}

.search-box {
  display: flex;
  margin-bottom: 20px;
}

.search-input {
  flex: 1;
  padding: 10px;
  font-size: 16px;
  border: 1px solid #ccc;
  border-radius: 4px 0 0 4px;
}

.search-button {
  padding: 10px 20px;
  background-color: #42b983;
  color: white;
  border: none;
  border-radius: 0 4px 4px 0;
  cursor: pointer;
}

.loading {
  text-align: center;
  padding: 20px;
}

.book-results {
  display: flex;
  flex-wrap: wrap;
  gap: 20px;
}

.book-card {
  width: calc(33.333% - 20px);
  border: 1px solid #eee;
  border-radius: 4px;
  padding: 15px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.edit-button {
  margin-top: 10px;
  padding: 8px 15px;
  background-color: #f1c40f;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

.edit-button:hover {
  background-color: #e0ac1f;
}

.edit-form {
  margin-top: 20px;
}

.form-group {
  margin-bottom: 15px;
}

.form-group label {
  display: block;
  margin-bottom: 5px;
}

.form-group input, .form-group textarea {
  width: 100%;
  padding: 10px;
  border: 1px solid #ccc;
  border-radius: 4px;
}

button {
  padding: 10px 20px;
  background: #42b983;
  color: #fff;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

button:hover {
  background: #3498db;
}

.cancel-button {
  margin-left: 10px;
  background-color: #e74c3c;
  color: white;
}

.cancel-button:hover {
  background-color: #c0392b;
}

.no-results {
  text-align: center;
  padding: 20px;
  color: #7f8c8d;
}
</style>